package com.gitee.cashzhang.test.basic.thread;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/06/05 17:23
 */
public class VolatileRunThread2 extends Thread {

  /**
   * volatile
   */
  private boolean isRunning = true;

  private void setRunning(boolean isRunning) {
    this.isRunning = isRunning;
  }

  public void run() {
    System.out.println("进入 run() 方法中...");
    while (isRunning == true) {
      // doSomething()
    }
    System.out.println("线程结束了...");
  }

  public static void main(String[] args) throws InterruptedException {
    VolatileRunThread2 myThread = new VolatileRunThread2();
    myThread.start();
    Thread.sleep(3000);
    myThread.setRunning(false);
    System.out.println("isRunning 的值已经设置为了 false");
    Thread.sleep(1000);
    System.out.println(myThread.isRunning);
  }
}
